package com.shine.reflect;


import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @program: reflect
 * @description:
 * @author: Shine
 * @create: 2019-07-24 08:53
 */
public class ReflectUtils<T> {

    /**
     * @Author: Shine
     * @Date: 19-7-24 上午8:48
     * @Description: 拿到对象的所有属性
     * @invoke: [t]
     * @return: java.util.List
     */
    @Deprecated
    public Map<String, Object> getAllPropertys(T t) throws IllegalAccessException {
        Map<String, Object> map = new ConcurrentHashMap();
        Class<?> z = t.getClass();
        Field[] fields = z.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            String name = field.getName();
            Object o = field.get(t);
            map.put(name, o);
        }
        return map;
    }

    /**
     * @Author: Shine
     * @Date: 19-7-24 上午9:49
     * @Description: 拿到属性中的单个属性值
     * @Param: [ts, fieldName]
     * @return: java.util.List<java.lang.Object>
     */
    public List<Object> getProperty(List<Object> ts, String fieldName) throws IllegalArgumentException, NoSuchFieldException, IllegalAccessException {
        List<Object> values = new ArrayList();
        for (Object t : ts) {
            Field field = t.getClass().getDeclaredField(fieldName);
            field.setAccessible(true);
            Object o = field.get(t);
            values.add(o);

        }
        return values;
    }

    /**
     * @Author: Shine
     * @Date: 19-7-24 上午9:49
     * @Description: 拿到单个对象集合中的多个属性值
     * @Param: [ts, fieldName]
     * @return: java.util.List<java.lang.Object>
     */
    public List<Map> getMultiProperty(List<T> ts, String... args) throws IllegalAccessException, NoSuchFieldException {
        List list=new ArrayList();

        for (T t : ts) {
            Map<String,Object> map=new HashMap();
            for (String arg : args) {
                Class<?> z = t.getClass();
                if(!isEmpty(arg)){
                    Field field = z.getDeclaredField(arg);
                    field.setAccessible(true);
                    String name = field.getName();
                    Object o = field.get(t);
                    map.put(name,o);
                }
            }
            list.add(map);
        }
        return list;
    }

    /**
     * @Author: Shine
     * @Date: 19-7-24 下午2:32
     * @Description: 拿到集合中的所有内容
     * @Param: [list]
     * @return: void
     */
    public  List<Map<String,Object>> getListProperty(List<Object> list) throws IllegalAccessException {
        List<Map<String,Object>> resultMap =new ArrayList<>();
        for (Object t : list) {
            Class<?> z = t.getClass();
            Field[] declaredFields = z.getDeclaredFields();
            Map<String, Object> fieldMap = new HashMap<>();
            for (Field declaredField : declaredFields) {
                declaredField.setAccessible(true);
                String name = declaredField.getName();
                Object o = declaredField.get(t);
                fieldMap.put(name,o);
            }
            resultMap.add(fieldMap);
        }
        return resultMap;
    }

    /**
     * @Author: Shine
     * @Date: 19-7-24 上午10:39
     * @Description: 字符串的非空判断
     * @Param: [str]
     * @return: boolean
     */
    public boolean isEmpty(String str) {
        if (str == null) {
            return true;
        }
        if ("".equals(str.trim())) {
            return true;
        }
        if (str.isEmpty()) {
            return true;
        }
        return false;
    }
}
